const app = getApp()
const util = require('../../utils/util.js')
const http = require('../../utils/http.js');
const Net = require('../../Net')
Page({
  data: {
    usrState: 0,
    //用户信息
    userInfo: '',
    money: '',
    points: '',
    //粉丝 
    fans_order_total: '',
    //粉丝订单
    fans_total: '',
    //订单参数
    order_total: '',
    //未领礼物
    receive_total: '',
    mobile: '',
    content: '',
    virtual_money: '',
    isShow: false,
    look: 0,
    info: {},
    tool: '',
    backTopImg: '',
    keyWorld: ''
  },
  //点击跳转登录
  JumpLogin: util.throttle(function (e) {
    wx.navigateTo({
      url: '/pages/Login/Index/index'
    })
  }, 1000),
  onPagePlus() {
    wx.navigateTo({
      url: '/pages/onNewPages/onPlusCip/onPlusCip',
    })
  },
  News() {
    console.log('/pages/onNewPages/official/official?Url=' + this.data.info.subscribe.kefu.link.apply_name + '&Path=' + this.data.info.subscribe.kefu.link.miniapp_path + '&Title=' + this.data.info.subscribe.kefu.link.title + '&Description=' + this.data.info.subscribe.kefu.link.describe + '&Imgurl=' + this.data.info.subscribe.kefu.link.image)
    wx.navigateTo({
      url: '/pages/onNewPages/official/official?card=' + this.data.keyWorld + '&title=' + this.data.info.subscribe.kefu.link.title
    })
  },
  Bill() {
    wx.navigateTo({
      url: '/pages/Login/Bill/Bill?bill_type_group=' + this.data.info.history_total.bill_type_group + '&bill_type_ids=' + this.data.info.history_total.bill_type_ids + '&title=' + this.data.info.history_total.bill_type_title,
    })
  },
  //个人中心1
  appIndex(data) {
    var _this = this
    if (wx.getStorageSync('v1/user')) {
      _this.appIndexSql(wx.getStorageSync('v1/user'))
    } else {
      http.getRequest('v1/user', data, function (res) {
          wx.setStorageSync('v1/user', res)
          _this.appIndexSql(res)
        },
        function (err) {
          console.log(err)
          _this.setData({
            backTopImg: '',
            usrState: 0
          })
          wx.clearStorage()
          if (wx.getStorageSync('token')) {
            _this.mytool('')
          }
          console.log('缓存导致接口错误，将要清除所有数据')
        })
    }
  },
  appIndexSql(res) {
    var _this = this,
      tool = _this.data.tool
    if (res.code == 200) {
      console.log(res, tool)
      tool.order_list.data_list = []
      tool.order_list.data_list = res.data.order_list
      console.log({
        userinfo: {
          nickName: wx.getStorageSync('member').member_oauth_info.nickname,
          avatarUrl: wx.getStorageSync('member').member_oauth_info.avatar_url
        },
        keyword: res.data.subscribe.kefu.link.keywords,
        ctag: res.data.subscribe.kefu.link.apply_name
      })
      _this.setData({
        usrState: 1,
        info: res.data,
        tool: tool,
        keyWorld: encodeURIComponent(JSON.stringify({
          userinfo: {
            nickName: wx.getStorageSync('member').member_oauth_info.nickname,
            avatarUrl: wx.getStorageSync('member').member_oauth_info.avatar_url
          },
          keyword: '公众号',
          ctag: res.data.subscribe.kefu.link.apply_name
        }))
      })
      if (res.data.background.substring(0, res.data.background.length - 11) != _this.data.backTopImg.substring(0, _this.data.backTopImg.length - 11)) {
        _this.setData({
          backTopImg: res.data.background
        })
      }
      var list = [];
      res.data.order_list.map((v, k) => {
        list.push({
          apply_name: res.data.order_list[k].apply_name,
          status: res.data.order_list[k].status,
        })
      })
      list.splice(0, 0, {
        apply_name: '全部',
        status: -1,
      })
      wx.setStorageSync('orderTap', list)
    }
  },
  onGoVip() {
    wx.navigateTo({
      url: '/pages/onNewPages/onUserOutPlus/onUserOutPlus?type=' + this.data.info.vip.apply_link.type,
    })
  },
  onOrderSearch() {
    wx.navigateTo({
      url: '/pages/onNewPages/orderSearch/orderSearch',
    })
  },
  //工具
  mytool(data) {
    var _this = this
    http.getRequest('v1/tool', data,
      function (res) {
        _this.setData({
          tool: res.data
        })
        _this.appIndex('')
      },
      function (err) {
        console.log('接口发生错误，此功能将清除缓存。')
      }, 'wechat')
  },
  notgift(e) {
    console.log(e)
    var _this = this
    let type = e.currentTarget.dataset.type;
    const token = wx.getStorageSync('token')
    if (!token && type != 'kefu') {
      wx.navigateTo({
        url: '/pages/Login/Index/index'
      })
      return false
    }
    if (e.currentTarget.id == 2) {
      wx.navigateTo({
        url: '/pages/onNewPages/onDownLoadFile/onDownLoadFile?url=' + type,
      })
      return false
    }
    switch (type) {
      case 'poster':
        wx.navigateTo({
          url: '/pages/onNewPages/onPostrtPage/onPostrtPage',
        })  
      break
      case 'dowload':
        var data = {},
          image = '',
          list = _this.data.tool.tool_list.data_list
        for (let i in list) {

          if (list[i].link.apply_name == 'dowload') {
            image = list[i].link.image
            data.keyword = list[i].link.keywords
            data.userinfo = {
              nickName: wx.getStorageSync('member').member_oauth_info.nickname,
              avatarUrl: wx.getStorageSync('member').member_oauth_info.avatar_url
            }
          }
        }
        console.log(data)
        wx.navigateTo({
          url: '/pages/onNewPages/onDwonLoadPages/onDwonLoadPages?card=' + encodeURIComponent(JSON.stringify(data)) + '&img=' + image
        })
        break;
      case 'team':
        wx.navigateTo({
          url: '/pages/onNewPages/myTeam/myTeam'
        })
        break;
        //设置
      case 'setting':
        wx.navigateTo({
          url: '/pages/Login/BindPhone/BindPhone'
        })
        break;
        //实名认证
      case "realname":
        var Info = wx.getStorageSync('Info');
        var renzheng = wx.getStorageSync('renzheng')
        if (Info.member_info == undefined && renzheng != '1') {
          wx.navigateTo({
            url: '/pages/Login/Certif/Certif',
          })
        } else {
          let data = wx.getStorageSync('shiming')
          let member = wx.getStorageSync('member')
          if (data) {
            wx.navigateTo({
              url: '/pages/onNewPages/CerSuc/CerSuc?data=' + JSON.stringify(data),
            })
          } else {
            wx.navigateTo({
              url: '/pages/onNewPages/CerSuc/CerSuc?data=' + JSON.stringify(member.member_info),
            })
          }
        }
        break;
        //客服电话
      case "kefu":
        wx.showModal({
          title: '温馨提示',
          content: '拨打' + Net.data('member').kefu + '电话？',
          success(res) {
            if (res.confirm) {
              wx.makePhoneCall({
                phoneNumber: Net.data('member').kefu,
              })
            }
          }
        })
        break;
      case "address":
        wx.navigateTo({
          url: '/pages/onNewPages/addressList/addressList',
        })
        break;
      case "store":
        if (!wx.getStorageSync('renzheng')) {
          wx.showModal({
            title: '温馨提示',
            content: '您还没有认证',
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/Login/Certif/Certif',
                })
              }
            }
          })
          return false
        }
        if (this.data.info.show_vip) {
          wx.showModal({
            title: '温馨提示',
            content: '您还没有开通超级创客',
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/onNewPages/onUserOutPlus/onUserOutPlus',
                })
              }
            }
          })
          return false
        }
        wx.navigateTo({
          url: '/pages/onNewPages/onOpenStroe/onOpenStroe',
        })
        break;
      case "mall":
        wx.navigateTo({
          url: '/pages/onNewPages/onOpenStroeSuccess/onOpenStroeSuccess?app_id=' + wx.getStorageSync('app_id') + '&page_path=' + wx.getStorageSync('page_path'),
        })
        break;

      case "live":
        wx.showToast({
          title: '开发中...',
          icon: 'none'
        })
        break;
    }
  },
  Profit() {
    wx.navigateTo({
      url: '/pages/onNewPages/Profit/Profit',
    })
  },
  //跳转二维码
  GetCode() {
    wx.navigateTo({
      url: '/pages/Login/getCode/getCode',
    })
  },
  _confirmEventFirst() {
    wx.makePhoneCall({
      phoneNumber: '400101168', //此号码并非真实电话号码，仅用于测试
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
    this.Modal.hideModal()
  },
  //获取邀请码
  getTel(e) {
    const code = e.currentTarget.dataset.code;
    let _this = this
    wx.setClipboardData({
      data: code,
      success: function (res) {
        wx.getClipboardData()
      }
    })
  },
  onLoad: function () {
    this.mytool('')
  },
  //跳转个BindPhone人信息
  BindPhone: util.throttle(function (e) {
    wx.navigateTo({
      url: '/pages/Login/BindPhone/BindPhone'
    })
  }, 1000),
  // 余额
  toBalance: util.throttle(function (e) {
    wx.navigateTo({
      url: '/pages/Login/Balance/Balance'
    })
  }, 1000),
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.toastDialog = this.selectComponent("#toastDialog"); //此处id和页面的 wxml中的id一致
    this.toasts = this.selectComponent('#toasts')
    this.Modal = this.selectComponent("#modal");

  },
  onShow: function () {
    if (wx.getStorageSync('member')) {
      var string = wx.getStorageSync('Info')
      if (wx.getStorageSync('member').member_oauth_info.nickname != string.userInfo.nickName) {
        string.userInfo.nickName = wx.getStorageSync('member').member_oauth_info.nickname
        wx.setStorageSync('Info', string)
      }
      this.setData({
        member: wx.getStorageSync('member')
      })
    }
    if (wx.getStorageSync('token')) {
      this.mytool('')
      this.setData({
        token: wx.getStorageSync('token')
      })
    } else {
      var info = this.data.info
      info.background = ''
      this.setData({
        usrState: 0,
        info: info
      })
    }
  },
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    wx.removeStorageSync('v1/user')
    this.appIndex('')
    wx.hideNavigationBarLoading()
    wx.stopPullDownRefresh()
  },
  gettab(e) {
    wx.navigateTo({
      url: '/pages/onNewPages/myOrder/myOrder?type=' + e.currentTarget.id,
    })
  }
})